import React, { Component } from 'react'

export default class Count extends Component {

  state = {
    count: 0
  }

  increament = () => {
    const { value } = this.selectNum
    const { count } = this.state
    this.setState({
      count: count + value * 1
    })
  }
  decreament = () => {
    const { value } = this.selectNum
    const { count } = this.state
    this.setState({
      count: count - value * 1
    })
  }
  increamentIfOdd = () => {
    const { value } = this.selectNum
    const { count } = this.state
    if (count % 2 !== 0) {
      this.setState({
        count: count + value * 1
      })
    }

  }
  increamentAsync = () => {
    const { value } = this.selectNum
    const { count } = this.state
    setTimeout(() => {
      this.setState({
        count: count + value * 1
      })
    }, 500)
  }

  render() {
    const { count } = this.state
    return (
      <div>
        <h1>当前求和为：{count}</h1>
        <select ref={c => this.selectNum = c} name="" id="">
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
        </select>
        <button onClick={this.increament}>+</button>
        <button onClick={this.decreament}>-</button>
        <button onClick={this.increamentIfOdd}>当前求和为奇数则加</button>
        <button onClick={this.increamentAsync}>异步加</button>
      </div>
    )
  }
}
